<?php
/**
 * 自己打分页面
*/
apf_require_controller("Abstract");
apf_require_class("Bll_Jx_Plan");
apf_require_class("Bll_Hr_Property");
class Plan_MarkScoreController extends AbstractController {

    private $view_status = array(
            Const_JxStatus::OPT_ACTION_30,//主管审核通过(计划)
            Const_JxStatus::OPT_ACTION_35,//主管驳回
            Const_JxStatus::OPT_ACTION_40,//自评打分送审（待主管审核）
            Const_JxStatus::OPT_ACTION_45,//部门经理驳回
            Const_JxStatus::OPT_ACTION_48,//主管审核待提交部门经理
            );

    public function handle_request() {
        $params = $this->request->get_parameters();
        $date = $params['searchDate'];
        //check view user
        $this->user_id = $this->request->get_user_id();
        $this->plan_bll = new Bll_Jx_Plan();

        $datalist = $this->plan_bll->get_plan_info($this->user_id,$date);
        $performance = empty($datalist['base_data']) ? array() : $datalist['base_data'];
        if(empty($performance)){
            $this->show_tips('ERROR_PARAM');
        }
        //这些阶段是不能操作的
        if(!in_array($performance['status'],$this->view_status)){
            $this->show_tips('ERROR_PARAM');
        }
        $performance_id = $datalist['base_data']['id'];


        $this->view_user_info = $this->plan_bll->get_user_info_from_kq($this->user_id);
        $this->my_user_id = 5652;
        //取最新领导,直接从OA接中获取
        $this->super_info = array('name'=>'李不太白');
        //$this->view_user_info = array();
        $this->startMonth = intval(substr($date, 4,2));
        $this->endMonth = intval(substr($date, 6,2));
        $this->searchDate = $date;
        $this->year = date('Y',strtotime(substr($date, 0,6).'01'));

        if($this->request->is_post_method()){
            $performance_id = $this->_post_performance($params);

            $this->_post_trial($performance_id, $params);

            $this->_post_kpi($performance_id, $params);

            $this->_post_review($performance_id, $params);

            $this->_post_culture($performance_id, $params);

            $this->plan_bll->count_self_super_score($performance_id);
        }

        //员工信息,是否试用期
        $is_trial = 0;
        if($this->view_user_info->indue_date !='0000-00-00 00:00:00' && time() < strtotime($this->view_user_info->indue_date) ){
            $is_trial =1;
        }
        $is_trial =1;

        $this->view_user_info->name='曹宏浪';
        $this->view_user_info->superior_code='SHF2938';
        $this->view_user_info->enter_date='2012-03-13 00:00:00';
        $this->view_user_info->department_id=15;
        $this->view_user_info->city_name = '上海';
        $this->view_user_info->group_name='总部';
        $this->view_user_info->department_name='技术部';
        $this->view_user_info->position_name='技术部111';



        //tp($datalist);
        //根据当前的状态，分别控制自己打分，主管打分,主管评价
        $performance = empty($datalist['base_data']) ? array() : $datalist['base_data'];


        $this->request->set_attribute('review_type_list', Const_JxStatus::review_type_list());
        $this->request->set_attribute('datalist', $datalist);
        $this->request->set_attribute('is_trial', $is_trial);
        #$this->request->set_attribute('is_self', $my_uid);
        $this->request->set_attribute('year', $this->year);
        $this->request->set_attribute('super_info', $this->super_info);
        $this->request->set_attribute('m_to_m', $this->startMonth.'-'.$this->endMonth);
        $this->request->set_attribute('view_user_info', $this->view_user_info);

        //这里判断是到什么状态
        if(1){
            return 'Plan_SelfScore';
        }else{
            return 'Plan_SuperScore';
        }

    }
    private function _post_performance(){
        $performance = $this->plan_bll->find_performance(array('userId'=>$this->user_id,'serachMonth'=>$this->searchDate));
        if(empty($performance)){
            $this->show_tips('ERROR_PARAM');
        }
        return $performance[0]['id'];
    }
    private function _post_trial($performance_id, $params){
        if(!empty($params['performanceActual1'])){
            foreach($params['performanceActual1'] as $k=>$val){
                $update = array(
                        'performanceActual'=>trim($val),
                        'personScore'=>$params['personScore1'][$k]
                        );
                $id = $params['detail1'][$k];
                $this->plan_bll->update_plan_view_by_id($id,$update);
            }
        }
        if(!empty($params['treasurerScore1'])){
            foreach($params['treasurerScore1'] as $k=>$val){
                $update = array(
                        'treasurerScore'=>$val,
                        'leadernote'=>$params['leadernote1'][$k]
                );
                $id = $params['detail1'][$k];
                $this->plan_bll->update_plan_view_by_id($id,$update);
            }
        }
        return true;
    }

    private function _post_kpi($performance_id, $params){
        if(!empty($params['performanceActual2'])){
            foreach($params['performanceActual2'] as $k=>$val){
                $update = array(
                        'performanceActual'=>trim($val),
                        'personScore'=>$params['personScore2'][$k]
                );
                $id = $params['detail2'][$k];
                $this->plan_bll->update_plan_view_by_id($id,$update);
            }
        }
        if(!empty($params['treasurerScore2'])){
            foreach($params['treasurerScore2'] as $k=>$val){
                $update = array(
                        'treasurerScore'=>$val,
                        'leadernote'=>$params['leadernote2'][$k]
                );
                $id = $params['detail2'][$k];
                $this->plan_bll->update_plan_view_by_id($id,$update);
            }
        }
        return true;
    }

    private function _post_review($performance_id, $params){
        if(!empty($params['performanceActual3'])){
            foreach($params['performanceActual3'] as $k=>$val){
                $update = array(
                        'performanceActual'=>trim($val),
                        'personScore'=>$params['personScore3'][$k]
                );
                $id = $params['detail3'][$k];
                $this->plan_bll->update_plan_view_by_id($id,$update);
            }
        }
        if(!empty($params['treasurerScore3'])){
            foreach($params['treasurerScore3'] as $k=>$val){
                $update = array(
                        'treasurerScore'=>$val,
                        'leadernote'=>$params['leadernote3'][$k]
                );
                $id = $params['detail3'][$k];
                $this->plan_bll->update_plan_view_by_id($id,$update);
            }
        }
        return true;
    }

    private function _post_culture($performance_id, $params){
        if(!empty($params['performanceActual4'])){
            foreach($params['performanceActual4'] as $k=>$val){
                $update = array(
                        'performanceActual'=>trim($val),
                        'personScore'=>$params['personScore4'][$k]
                );
                $id = $params['detail4'][$k];
                $this->plan_bll->update_plan_view_by_id($id,$update);
            }
        }
        if(!empty($params['treasurerScore4'])){
            foreach($params['treasurerScore4'] as $k=>$val){
                $update = array(
                        'treasurerScore'=>$val,
                        'leadernote'=>$params['leadernote4'][$k]
                );
                $id = $params['detail4'][$k];
                $this->plan_bll->update_plan_view_by_id($id,$update);
            }
        }
        return true;
    }
}